import { t } from '@/i18n';

export const commonReg = {
  code: /^\w+$/,
  code2: /^[\w.-]+$/,
  password: /^(?![0-9]+$)(?![a-z]+$)(?![A-Z]+$)[a-zA-Z0-9]{6,20}$/,
  phoneCode: /^[\d()\-\+\s]+$/,
  email: /^\w+((.\w+)|(-\w+))@[A-Za-z0-9]+((.|-)[A-Za-z0-9]+).[A-Za-z0-9]+$/,
  zhCode: /^[^\s\u4E00-\u9FA5]+$/,
  userLimit: /^(([1-9][0-9]{0,4})|99999)$/,

  msTime: /(^[1-9][0-9]{3,250}$)/,
  serviceCode: /^[^\s\u4E00-\u9FA5]+$/,
  levelCode: /^[A-Za-z0-9]+$/,

  modelLimit: /^(([0-9][0-9]{0,4})|99999)$/,
};

export const matching = (value = '', required = true, type = 'input', regList: any = []) => {
  if (!value) {
    if (required) {
      return new Error(t(`common.formTipMsg.${type}`));
    } else {
      return true;
    }
  } else {
    if (regList.length) {
      let errMsg = null;

      const checked = regList.every((item: any) => {
        const { msg = '', reg = null } = item;

        if (reg && !reg.test(value)) {
          errMsg = msg;

          return false;
        }

        return true;
      });

      if (checked) {
        return true;
      } else {
        return new Error(`${errMsg}`);
      }
    } else {
      return true;
    }
  }
};

/**
 * 验证类型：数字、字母、下划线
 * @param {string | number} value
 * @param {boolean} required 是否必填
 */
export function checkCodeFn(value, required) {
  return matching(value, required, 'input', [
    { reg: commonReg['code'], msg: t(`common.formTipMsg.code`) },
  ]);
}

/**
 * 验证类型：数字、字母、下划线、中划线和点
 * @param {string | number} value
 * @param {boolean} required 是否必填
 */
export function checkCode2Fn(value, required) {
  return matching(value, required, 'input', [
    { reg: commonReg['code2'], msg: t(`common.formTipMsg.code2`) },
  ]);
}

/**
 * 验证类型：最少包含数字、小写字母、大写字母两种，且符合长度要求
 * @param {string | number} value
 * @param {boolean} required 是否必填
 */
export const checkPassWordFn = (value, required) => {
  return matching(value, required, 'input', [
    {
      reg: commonReg['password'],
      msg: t(`common.formTipMsg.password`),
    },
  ]);
};

/**
 * 验证类型：电话号码
 * @param {string | number} value
 * @param {boolean} required 是否必填
 */
export const checkPhoneCodeFn = (value, required) => {
  return matching(value, required, 'input', [
    {
      reg: commonReg['phoneCode'],
      msg: t(`common.formTipMsg.phoneNo`),
    },
  ]);
};

/**
 * 验证类型：邮箱
 * @param {string | number} value
 * @param {boolean} required 是否必填
 */
export const checkEmailFn = (value, required) => {
  return matching(value, required, 'input', [
    {
      reg: commonReg['email'],
      msg: t(`common.formTipMsg.email`),
    },
  ]);
};

/**
 * 验证类型：不支持中文和空格
 * @param {string | number} value
 * @param {boolean} required 是否必填
 */
export function checkZhCodeFn(value, required) {
  return matching(value, required, 'input', [
    { reg: commonReg['zhCode'], msg: t(`common.formTipMsg.zhCode`) },
  ]);
}
//  * 验证类型：最大用户数
//  * @param {string | number} value
//  * @param {boolean} required 是否必填
//  */
export const checkUserLimitFn = (value, required) => {
  return matching(value, required, 'input', [
    {
      reg: commonReg['userLimit'],
      msg: t(`common.formTipMsg.userLimit`),
    },
  ]);
};

/**
 * 验证类型：等级编码
 * @param {string | number} value
 * @param {boolean} required 是否必填
 */
export const checkServerCodeFn = (value, required) => {
  return matching(value, required, 'input', [
    {
      reg: commonReg['serviceCode'],
      msg: t(`common.formTipMsg.serviceCode`),
    },
  ]);
};

/**
 * 验证类型：等级编码
 * @param {string | number} value
 * @param {boolean} required 是否必填
 */
export const checklevelCodeFn = (value, required) => {
  return matching(value, required, 'input', [
    {
      reg: commonReg['levelCode'],
      msg: t(`common.formTipMsg.levelCode`),
    },
  ]);
};

//  * 验证类型：验证时间，毫秒
//  * @param {string | number} value
//  * @param {boolean} required 是否必填
//  */
export const checkmsTimeFn = (value, required) => {
  return matching(value, required, 'input', [
    {
      reg: commonReg['msTime'],
      msg: t(`common.formTipMsg.msTime`),
    },
  ]);
};

export function checkStock(value, required) {
  return matching(value, required, 'input', [
    { reg: commonReg['modelLimit'], msg: t(`common.formTipMsg.stockLimit`) },
  ]);
}
